package com.y.controller;

import com.y.pojo.People;
import com.y.service.IPeopleService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PeopleController {
    @Autowired
    private IPeopleService peopleService;
    @RequestMapping("/queryPeople")
    public ModelAndView queryPeople(People people, ModelAndView mav, HttpServletRequest request){
        //加密
        String md5Hex = DigestUtils.md5Hex(people.getPid()+people.getPpass());
        people.setPpass(md5Hex);


        try {
            People people1 = peopleService.queryPeople(people);
           if (people1!=null){
               int state = people1.getState();
               System.out.println("st"+state);


               request.getSession().setAttribute("people",people1);
               return new ModelAndView("redirect:/queryHouse");
           }else{
               mav.setViewName("fail");
               return mav;
           }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }
    @RequestMapping("/insertPeople")
    public ModelAndView insertPeople(People people,ModelAndView mav){

        //加密
        String md5Hex = DigestUtils.md5Hex(people.getPid()+people.getPpass());
        people.setPpass(md5Hex);
        try {
            int i = peopleService.insertPeople(people);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mav.setViewName("login");
        return mav;
    }

    @RequestMapping("/selPeople")
    public ModelAndView selPeople(ModelAndView mav){
        mav.setViewName("login");
        return mav;
    }

    @RequestMapping("/addPeople")
    public ModelAndView addPeople(ModelAndView mav){
        mav.setViewName("regist");
        return mav;
    }

    @RequestMapping("/pay")
    public ModelAndView pay(HttpSession session,ModelAndView modelAndView){
        People people = (People) session.getAttribute("people");
        try {
            int i = peopleService.updatePeople(people);

            people.setState(1);
            session.setAttribute("people",people);
            System.out.println("i::::"+i);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return new ModelAndView("redirect:/queryHouse");
    }

}
